/**
 * 查询
 */
function search() {
	$("#activeName").val($.trim($("#activeName").val()));
	$("#search_form").submit();
}

/**
 * 重置查询条件
 */
function toIsEmpty() {
	$("#startDate").val("");
	$("#endDate").val("");
	$("#activeName").val("");
	$("#status").val("");
	$("#activeType").val("");
}

/**
 * 新增活动
 */
function addActive() {
	openLayer("发布活动", path + "/presentActivityController/to_add_activity");
}

/**
 * 更新活动信息
 * @param id
 */
function editActive(id) {
	openLayer("修改活动", path + "/presentActivityController/to_edit_activity?id=" + id);
}

/**
 * 查看活动信息
 * @param id
 */
function lookActive(id) {
	openLayer("查看活动", path + "/presentActivityController/to_look_activity?id=" + id);
}

/**
 * 审核活动
 * @param id
 */
function auditActive(id) {
	openLayer("审核活动", path + "/presentActivityController/to_audit_activity?id=" + id);
}

/**
 * 删除活动
 * @param id
 */
function deleteActive(id) {
	layer.confirm("确定删除此活动？", function() {
		layer.closeAll('dialog');
		var index = layer.load(2); 
		$.ajax({
			type: 'post',
			url: path + "/presentActivityController/delete_activity?id=" + id,
			dataType: 'text',
			success: function(data) {
				layer.close(index);
				if(data == "success") {
					layer.msg('删除成功！', {icon: 1, time: 500}, function() {
						$("#search_form").submit();
					});
				} else {
					layer.alert(data);
				}
			},
			error: function() {
				layer.close(index);
				layer.alert("操作失败！");
			}
		});		
	});
}

/**
 * 关闭活动
 * @param id
 */
function closeActive(id) {
	layer.confirm("确定要关闭此活动？", function() {
		layer.closeAll("dialog");
		var loadLayerIndex = layer.load(2); 
		$.ajax({
			type: "post",
			url: path + "/presentActivityController/closeActive",
			data: {id: id},
			dataType: "text",
	        success: function(data) {
		   		if(data == "success") {
		   			layer.msg("关闭成功！", {icon: 1, time: 500}, function() {
		   				search();
		   			});
		   		} else {
		   			layer.alert(data);
		   		}
			},
			error: function() {
				layer.alert("操作失败！");
	        },
			complete: function(XMLHttpRequest, textStatus) {
	        	layer.close(loadLayerIndex);
	        }
		});		
	});
}

/**
 * 暂停活动
 * @param id
 */
function pauseActive(id) {
	layer.confirm("确定要暂停此活动？", function() {
		layer.closeAll("dialog");
		var loadLayerIndex = layer.load(2); 
		$.ajax({
			type: "post",
			url: path + "/presentActivityController/pauseActive",
			data: {id: id},
			dataType: "text",
	        success: function(data) {
		   		if(data == "success") {
		   			layer.msg("暂停成功！", {icon: 1, time: 500}, function() {
		   				search();
		   			});
		   		} else {
		   			layer.alert(data);
		   		}
			},
			error: function() {
				layer.alert("操作失败！");
	        },
			complete: function(XMLHttpRequest, textStatus) {
	        	layer.close(loadLayerIndex);
	        }
		});		
	});
}

/**
 * 开始活动
 * @param id
 */
function startActive(id) {
	layer.confirm("确定要开始此活动？", function() {
		layer.closeAll("dialog");
		var loadLayerIndex = layer.load(2); 
		$.ajax({
			type: "post",
			url: path + "/presentActivityController/startActive",
			data: {id: id},
			dataType: "text",
	        success: function(data) {
		   		if(data == "success") {
		   			layer.msg("开始成功！", {icon: 1, time: 500}, function() {
		   				search();
		   			});
		   		} else {
		   			layer.alert(data);
		   		}
			},
			error: function() {
				layer.alert("操作失败！");
	        },
			complete: function(XMLHttpRequest, textStatus) {
	        	layer.close(loadLayerIndex);
	        }
		});		
	});
}

/**
 * 打开弹出层
 * @param title 标题
 * @param url 访问URL
 * @return
 */
function openLayer(title, url) {
	var index = layer.open({
		type: 2,
		title: title,
		content: url,
		area: [(document.body.clientWidth > 1000 ? 1000 : document.body.clientWidth) + "px", (document.body.clientHeight > 600 ? 600 : document.body.clientHeight) + "px"]
    });
	// 最大化
    layer.full(index);
    return index;
}